<?php
class Model_User_Group extends CI_Model {
	public function get_specific_all($num = 0, $offset = 0, $sort = '', $keyarray = array())
	{
		$this->db->from('user_group u');
		
		if($sort)
		{
			$this->db->order_by($sort);
		}
		else
		{
			$this->db->order_by('u.user_group_name', 'ASC');
		}
		
		if ($offset < 0) {
			$offset = 0;
		}				

		if ($num < 1) {
			$num = 20;
		}	
		
		$this->db->limit((int)$num, (int)$offset);
		
		return $this->db->get()->result_array();
	}
	
	public function get_specific_count_all() {
		
		$this->db->select('COUNT(*) AS total');
		$this->db->from('user_group u');
		
		return $this->db->get()->row()->total;
	}	
	
	public function addUserGroup($data) {
		$this->db->query("INSERT INTO " . $this->db->dbprefix . "user_group SET user_group_name = " . $this->db->escape($data['name']) . ", permission = '" . ''/*(isset($data['permission']) ? serialize($data['permission']) : '')*/ . "'");
	}
	
	public function editUserGroup($user_group_id, $data) {
		$this->db->query("UPDATE " . $this->db->dbprefix . "user_group SET user_group_name = " . $this->db->escape($data['name']) . ", permission = '" . (isset($data['permission']) ? serialize($data['permission']) : '') . "' WHERE user_group_id = '" . (int)$user_group_id . "'");
	}
	
	public function deleteUserGroup($user_group_id) {
		$this->db->query("DELETE FROM " . $this->db->dbprefix . "user_group WHERE user_group_id = '" . (int)$user_group_id . "'"); 
		
		return true;
	}

	public function addPermission($user_id, $type, $page) {
		$user_query = $this->db->query("SELECT DISTINCT user_group_id FROM " . $this->db->dbprefix . "user WHERE user_id = '" . (int)$user_id . "'");
		
		if ($user_query->num_rows()) {
			$user_group_query = $this->db->query("SELECT DISTINCT * FROM " . $this->db->dbprefix . "user_group WHERE user_group_id = '" . (int)$user_query->row('user_group_id') . "'");
		
			if ($user_group_query->num_rows()) {
				$data = unserialize($user_group_query->row('permission'));
		
				$data[$type][] = $page;
		
				$this->db->query("UPDATE " . $this->db->dbprefix . "user_group SET permission = '" . serialize($data) . "' WHERE user_group_id = '" . (int)$user_query->row('user_group_id') . "'");
			}
		}
	}
	
	public function getUserGroup($user_group_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . $this->db->dbprefix . "user_group WHERE user_group_id = '" . (int)$user_group_id . "'");
		
		$user_group = array(
			'name'       => $query->row()->user_group_name,
			'permission' => unserialize($query->row()->permission)
		);
		
		return $user_group;
	}
	
	public function getUserGroups($data = array()) {
		$sql = "SELECT * FROM " . $this->db->dbprefix . "user_group";
		
		$sql .= " ORDER BY name";	
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
		
		return $query->result_array();
	}
	
}
?>